package com.sdkj.dspservice.modules.sys.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdkj.dspservice.common.base.entity.BaseResult;
import com.sdkj.dspservice.common.constant.HttpStatus;
import com.sdkj.dspservice.common.react.AntdProResult;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.modules.sys.entity.Dict;
import com.sdkj.dspservice.modules.sys.service.DictService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典表Controller
 *
 * @author tm
 */
@RestController
@RequestMapping(value = "${project-config.apiUrl}/${project-config.version}/sys/dict")
public class DictController {

    @Autowired
    private DictService dictService;

//    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "get")
    public AntdProResult get(String id) {
        AntdProResult result = new AntdProResult();
        if (StringUtils.isNotBlank(id)) {
            Dict dict = dictService.getById(id);
            result.setData(dict);
        } else {
            result.error(HttpStatus.STATUS_400, "id不能为空");
        }
        return result;
    }

//    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "list")
    public AntdProResult list(Dict dict) {
        AntdProResult result = new AntdProResult();
        List<Dict> dictList = dictService.list(dict);
        result.success(dictList);
        return result;
    }

//    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "page")
    public AntdProResult page(Dict dict) {
        AntdProResult result = new AntdProResult();
        IPage<Dict> dictIPage = dictService.page(new Page<>(dict.current, dict.pageSize), new QueryWrapper<>(dict));
        result.success(dictIPage);
        return result;
    }

//    @RequiresPermissions("sys:dict:edit")
    @RequestMapping(value = "save", produces = MediaType.APPLICATION_JSON_VALUE)
    public AntdProResult save(@Valid @RequestBody Dict dict) {
        AntdProResult result = new AntdProResult();
        dictService.saveOrUpdate(dict);
        return result;
    }

//    @RequiresPermissions("sys:dict:edit")
    @RequestMapping(value = "remove")
    public BaseResult delete(String id, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        if (StringUtils.isNotBlank(id)) {
            dictService.removeById(id);
        } else {
            response.setStatus(HttpStatus.STATUS_400);
            result.setErrorMessage("id不能为空");
        }
        return result;
    }

    /**
     * 以单个Map(k-v)形式返回，k为value，v为label
     * @param dict
     * @return
     */
//    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "map")
    public AntdProResult map(Dict dict) {
        AntdProResult result = new AntdProResult();
        List<Dict> dictList = dictService.list(dict);
        // 组装成map
        Map<String, String> map = new HashMap<>();
        for(Dict d : dictList){
            map.put(d.getValue(), d.getLabel());
        }
        result.success(map);
        return result;
    }

    /**
     * 获取数据源select列表
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取select列表")
    @GetMapping(value = "listForSelect")
    public AntdProResult listForSelect(Dict dict) {
        AntdProResult result = new AntdProResult();
        List<Dict> dictList = dictService.list(dict);
        // 转换为{label:xxx,value:xxx}
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Dict r : dictList) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("label", r.getLabel());
            map.put("value", r.getValue());
            mapList.add(map);
        }
        result.success(mapList);
        return result;
    }

}